package com.epoint.mobileoa.ui.mail;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.adapter.AttachManagerListAdp;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.bizlogic.mobileoa.SendMailNoAttachTask;
import com.epoint.mobileoa.bizlogic.mobileoa.SendMailWithAttachTask;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.http.HttpHelp;
import com.epoint.mobileoa.model.Draft;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.DateHelp;
import com.epoint.mobileoa.tools.EpointDialog;
import com.epoint.mobileoa.tools.IOHelp;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.tools.PropertiesHelp;
import com.epoint.mobileoa.tools.SDCard;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;
import com.epoint.mobileoa.ui.attachmanager.AttachListView;
import com.epoint.mobileoa.ui.audiorecord.AudioRecordView;
import com.epoint.mobileoa.ui.filechoose.FileChooseView;
import com.epoint.mobileoa.ui.init.InitView;
import com.epoint.mobileoa.ui.personchoose.PersonChooseViewNew;
import com.epoint.mobileoa.ui.photo.PhotoListView;

/**
 * @author liyc
 * @time 2012-2-3 下午7:51:22
 * @annotation 邮件发送
 */
public class MailSendView extends SuperView {

	public static final int SendMailNoAttach = 1;

	private int resumeType = 5;// 0收件人恢复，1抄送人恢复，2附件恢复，3照片恢复，4录音恢复,5其他地方进来

	private int pagetype;// 0写邮件,1草稿箱详情,2邮件回复,3转发,4问题反馈

	private Button sjr_bt;// 收件人选择按钮
	private Button csr_bt;// 抄送人选择按钮
	private EditText etSJR;// 收件人文本框
	private EditText etCJR;// 收件人文本框
	private EditText etMailTitle;// 邮件标题文本框
	private EditText etMailContent;// 邮件正文文本框

	private Button btAttach;// 添加附件按钮
	private Button btCamera;// 添加照片按钮
	private Button btAudio;// 添加录音按钮
	private Button btSendMail;// 邮件发送按钮

	private String sjrnames;// 收件人姓名
	private String sjrguids;// 收件人guid
	private String csrnames;// 抄送人姓名
	private String csrguids;// 抄送人guid

	private int attachNums;// 附件数
	private String attachNames = "";// 附件名，分号隔开
	private String attachPaths = "";// 附件路径，分号隔开

	private String draftguid;

	private Button bt_add_attach_photo;

	private RelativeLayout om_rl_chaosongren;// 抄送区域
	private Button btDisplayCS;// 显示抄送按钮

	private ListView lvAttach;

	private String yuanMailContent;

	public static final int NONE = 0;
	public static final int PHOTOHRAPH = 1;// 拍照
	public static final int PHOTOZOOM = 2; // 缩放
	public static final int PHOTORESOULT = 3;// 结果
	public static final String IMAGE_UNSPECIFIED = "image/*";

	private String sendingmailmsg = "正在发送邮件...";
	private String sendSuccessMsg = "邮件发送成功！";
	private String sendFailureMsg = "邮件发送失败，请重试！";
	private String inputMailContentMsg = "请填写邮件内容！";

	@Override
	public void initControls() {
		sjr_bt = (Button) findViewById(R.id.find_receiver_btn1);
		csr_bt = (Button) findViewById(R.id.find_receiver_btn2);
		etSJR = (EditText) findViewById(R.id.mail_firstreceiver);
		etCJR = (EditText) findViewById(R.id.mail_secondreceiver);
		etMailContent = (EditText) findViewById(R.id.mail_content);
		etMailTitle = (EditText) findViewById(R.id.mail_title);
		btAttach = (Button) findViewById(R.id.bt_add_attach);
		btAttach.setOnClickListener(this);
		btCamera = (Button) findViewById(R.id.bt_add_attach_photo);
		btAudio = (Button) findViewById(R.id.bt_add_attach_audio);
		btSendMail = (Button) findViewById(R.id.sendEmail_btn);

		bt_add_attach_photo = (Button) findViewById(R.id.bt_add_attach_photo);
		bt_add_attach_photo.setOnClickListener(this);

		om_rl_chaosongren = (RelativeLayout) findViewById(R.id.om_rl_chaosongren);
		btDisplayCS = (Button) findViewById(R.id.bt_display_cs);
		btDisplayCS.setOnClickListener(this);

		sjr_bt.setOnClickListener(this);
		csr_bt.setOnClickListener(this);
		btSendMail.setOnClickListener(this);
		top_right_bt.setVisibility(View.VISIBLE);
		top_right_bt.setText("保存");
		top_right_bt.setOnClickListener(this);
		top_left_bt.setOnClickListener(this);

		lvAttach = (ListView) findViewById(R.id.lvattachlist);
		lvAttach.setOnItemClickListener(this);
		lvAttach.setOnItemLongClickListener(this);

		if (PropertiesHelp.getProperty(this, "mail_attach").equals("0")) {
			btAttach.setVisibility(View.INVISIBLE);
			btCamera.setVisibility(View.INVISIBLE);
			btAudio.setVisibility(View.INVISIBLE);
		}

		pagetype = getIntent().getIntExtra("pagetype", 0);

		if (pagetype == 0) {
			top_title.setText("写邮件");
		} else if (pagetype == 1) {

			top_title.setText(getIntent().getStringExtra("viewtitle"));
			etMailTitle.setText(getIntent().getStringExtra("mailtitle"));
			etMailContent.setText(getIntent().getStringExtra("mailcontent"));
			sjrnames = getIntent().getStringExtra("sjrnames");
			sjrguids = getIntent().getStringExtra("sjrguids");
			csrnames = getIntent().getStringExtra("csrnames");
			csrguids = getIntent().getStringExtra("csrguids");
			draftguid = getIntent().getStringExtra("draftguid");
			attachNames = getIntent().getStringExtra("attachname") == null ? ""
					: getIntent().getStringExtra("attachname");
			attachPaths = getIntent().getStringExtra("attachpath") == null ? ""
					: getIntent().getStringExtra("attachpath");

			if (!attachNames.equals("")) {
				String[] names = attachNames.split(";");
				String[] paths = attachPaths.split(";");

				for (int i = 0; i < names.length; i++) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("name", names[i]);
					map.put("path", paths[i]);
					map.put("icon", R.drawable.img_attachlist);
					attachmData.add(map);
				}
				MyAdapter adapter = new MyAdapter(this);
				lvAttach.setAdapter(adapter);
			}
			etSJR.setText(sjrnames);
			etCJR.setText(csrnames);

			if (!StringHelp.isEditTextBlank(etCJR)) {
				om_rl_chaosongren.setVisibility(View.VISIBLE);
				btDisplayCS.setText("取消抄送");
			}
		} else if (pagetype == 2) {
			top_title.setText("邮件回复");
			etMailTitle.setText("回复:"
					+ getIntent().getStringExtra("replaytitle"));
			sjrnames = getIntent().getStringExtra("mailsender");
			sjrguids = getIntent().getStringExtra("mailsenderguid");
			etSJR.setText(sjrnames);

		} else if (pagetype == 3) {
			top_title.setText("邮件转发");
			etMailTitle.setText("转发:"
					+ getIntent().getStringExtra("replaytitle"));
			// String zfcontent = getIntent().getStringExtra("mailcontent");
			// etMailContent.setText(Html.fromHtml(zfcontent));
			btAttach.setVisibility(View.GONE);
			btCamera.setVisibility(View.GONE);
			btAudio.setVisibility(View.GONE);
			top_right_bt.setVisibility(View.INVISIBLE);
			etSJR.setText(sjrnames);
			yuanMailContent = getIntent().getStringExtra("mailcontent");
		} else if (pagetype == 4) {
			sendingmailmsg = "正在反馈问题...";
			sendSuccessMsg = "问题反馈成功！";
			sendFailureMsg = "问题反馈失败，请重试！";
			etMailContent.setHint("添加反馈信息");
			inputMailContentMsg = "请添加反馈信息内容！";
			btSendMail.setText("提交");
			top_title.setText(getIntent().getStringExtra("viewtitle"));

			etMailTitle.setText(getIntent().getStringExtra("mailtitle"));
			etMailTitle.setEnabled(false);
			sjrnames = getIntent().getStringExtra("sjrnames");
			sjrguids = getIntent().getStringExtra("sjrguids");
			etSJR.setText(sjrnames);
			findViewById(R.id.om_rl_accepter).setVisibility(View.GONE);
			btDisplayCS.setVisibility(View.GONE);
			top_right_bt.setVisibility(View.GONE);
			btAttach.setText("上传日志文件");
			btAttach.setBackgroundResource(R.drawable.button_login);
		}
	}

	@Override
	public void onClick(View v) {
		if (v == top_right_bt) {
			if (StringHelp.isEditTextBlank(etMailTitle)
					&& StringHelp.isEditTextBlank(etMailContent)) {
				ToastHelp.showToastCommon(this, "请填写邮件标题或邮件内容再保存！");
			} else {
				Draft df = new Draft();
				if (draftguid == null) {
					draftguid = UUID.randomUUID().toString();
				}
				df.draftguid = draftguid;
				df.mailtitle = etMailTitle.getText().toString();
				df.draftsavetime = DateHelp.convertDate(new Date(),
						"yyyy-MM-dd HH:mm:ss");
				df.sendername = fp.getDisplayname();
				df.senderguid = fp.getUserguid();
				df.receivername = sjrnames;
				df.receiveguid = sjrguids;
				df.chaosongrename = csrnames;
				df.chaosongrenguid = csrguids;
				df.mailcontent = etMailContent.getText().toString();
				attachNums = attachmData.size();
				df.hasattach = attachNums == 0 ? "0" : "1";

				attachNames = "";
				attachPaths = "";
				for (int i = 0; i < attachmData.size(); i++) {
					attachNames += attachmData.get(i).get("name") + ";";
					attachPaths += attachmData.get(i).get("path") + ";";
				}

				df.attachname = attachNames;
				df.attachpath = attachPaths;
				DBHelp.setDraft(this, df);
				ToastHelp.showToastCommon(this, "草稿保存成功！");
			}
		} else if (v == sjr_bt) {
			resumeType = 0;
			PersonChooseViewNew.clist = StringHelp.String2List(sjrnames,
					sjrguids);
			Intent it = new Intent(this, PersonChooseViewNew.class);
			startActivity(it);
		} else if (v == csr_bt) {
			resumeType = 1;
			PersonChooseViewNew.clist = StringHelp.String2List(csrnames,
					csrguids);
			Intent it = new Intent(this, PersonChooseViewNew.class);
			startActivity(it);
		} else if (v == btSendMail) {
			if (etMailTitle.getText().toString().trim().length() == 0) {
				ToastHelp.showToastCommon(this, "请填写邮件标题！");
			} else if (etSJR.getText().toString().trim().length() == 0) {
				ToastHelp.showToastCommon(this, "请选择收件人！");
			} else if (etMailContent.getText().toString().trim().length() == 0&&pagetype!=3) {
				ToastHelp.showToastCommon(this, inputMailContentMsg);
			} else {

				showDialog_wait(sendingmailmsg);
				attachNums = attachmData.size();

				if (attachNums == 0) {
					HashMap<String, Object> param = new HashMap<String, Object>();
					String mailcontent = etMailContent.getText().toString()
							.replaceAll("\n", "<br/>");
					param.put("mailtitle", etMailTitle.getText().toString());

					if (pagetype == 2 || pagetype == 3) {
						param.put("mailcontent", mailcontent);
						String mailsendinfo = "<b>发件人：</b>"
								+ getIntent().getStringExtra("mailsender")
								+ "<br><br>";
						mailsendinfo += "<b>发件时间：</b>"
								+ getIntent().getStringExtra("mailtime")
								+ "<br><br>";
						mailsendinfo += "<b>主题：</b>"
								+ getIntent().getStringExtra("replaytitle")
								+ "<br><br>";// replaytitle
						yuanMailContent = getIntent().getStringExtra(
								"mailcontent");
						param.put("mailcontent", mailcontent
								+ "<br><br><hr><br><br>" + mailsendinfo
								+ yuanMailContent);
					} else {
						param.put("mailcontent", mailcontent);
					}

					if (pagetype == 3) {
						param.put("CliengGuid",
								getIntent().getStringExtra("CliengGuid"));
						param.put("zfhasattach",
								getIntent().getStringExtra("hasattach"));
					}

					param.put("sjrnames", sjrnames);
					param.put("sjrguids", sjrguids);
					param.put("csrnames", csrnames == null ? "" : csrnames);
					param.put("csrguids", csrguids == null ? "" : csrguids);
					Task task = new SendMailNoAttachTask(this, param);
					MainService.addTask(task);
				} else {

					HashMap<String, Object> param = new HashMap<String, Object>();
					String mailcontent = etMailContent.getText().toString()
							.replaceAll("\n", "<br/>");
					param.put("mailtitle", etMailTitle.getText().toString());

					if (pagetype == 2 || pagetype == 3) {
						param.put("mailcontent", mailcontent);
						String mailsendinfo = "<b>发件人：</b>"
								+ getIntent().getStringExtra("mailsender")
								+ "<br><br>";
						mailsendinfo += "<b>发件时间：</b>"
								+ getIntent().getStringExtra("mailtime")
								+ "<br><br>";
						mailsendinfo += "<b>主题：</b>"
								+ getIntent().getStringExtra("replaytitle")
								+ "<br><br>";// replaytitle
						yuanMailContent = getIntent().getStringExtra(
								"mailcontent");
						param.put("mailcontent", mailcontent
								+ "<br><br><hr><br><br>" + mailsendinfo
								+ yuanMailContent);
					} else {
						param.put("mailcontent", mailcontent);
					}

					param.put("sjrnames", sjrnames);
					param.put("sjrguids", sjrguids);
					param.put("csrnames", csrnames == null ? "" : csrnames);
					param.put("csrguids", csrguids == null ? "" : csrguids);

					Map<String, File> files = new HashMap<String, File>();

					for (int i = 0; i < attachmData.size(); i++) {
						File file = new File(attachmData.get(i).get("path")
								.toString());
						files.put(attachmData.get(i).get("name").toString(),
								file);
					}

					param.put("attaches", files);

					Task task = new SendMailWithAttachTask(this, param);
					MainService.addTask(task);
				}
			}
		} else if (v == top_left_bt) {
			back();
		} else if (v == bt_add_attach_photo) {

		} else if (v == btDisplayCS) {
			if (btDisplayCS.getText().equals("添加抄送")) {
				om_rl_chaosongren.setVisibility(View.VISIBLE);
				btDisplayCS.setText("取消抄送");
			} else {
				om_rl_chaosongren.setVisibility(View.GONE);
				btDisplayCS.setText("添加抄送");
				csrguids = "";
				csrnames = "";
				etCJR.setText("");
			}
		} else if (v == btAttach) {
			resumeType = 2;

			if (pagetype == 4) {// 问题反馈
				Intent it = new Intent(this, FileChooseView.class);
				it.putExtra("initPath", "/" + InitView.MobileOaMainStoreFolder
						+ "/mobileoa/log");
				startActivity(it);
				return;
			}

			showAttachMenu();
		}
	}

	/**
	 * @author lilin
	 * @date 2012-5-11 上午11:08:20
	 * @annotation
	 */
	private void showAttachMenu() {
		String item[] = { "内存卡", "已下载附件" };
		String item2[] = { "内存卡", "已下载", "拍照", "录音", "录像" };
		String item3[];
		if (PropertiesHelp.getProperty(this, "isPhotoAttach").equals("1")) {
			item3 = item2;
		} else {
			item3 = item;
		}
		resumeType = 2;
		AlertHelp.showAlertMenu(this, "附件来自于：", item3, new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				case 0:
					Intent it = new Intent(MailSendView.this,
							FileChooseView.class);
					if (pagetype == 4) {// 问题反馈
						it.putExtra("initPath", "/"
								+ InitView.MobileOaMainStoreFolder
								+ "/mobileoa/log");
					}
					startActivity(it);
					break;
				case 1:
					startActivity(new Intent(MailSendView.this,
							AttachListView.class));
					break;
				case 2:
					// startActivity(new Intent(MailSendView.this,
					// TakePhotoView.class));//调用自定义相机
					callSystemCamera();// 调用系统相机
					break;
				case 3:
					startActivity(new Intent(MailSendView.this,
							AudioRecordView.class));
					break;
				case 4:
					callSystemVedioRecord();// 调用系统相机
					break;
				default:
					break;
				}
			}
		});
	}

	/**
	 * @author lilin
	 * @date 2012-12-10 上午10:44:07
	 * @annotation 调用系统录像
	 */
	protected void callSystemVedioRecord() {
		Intent mIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		mIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0.5);// 画质0.5
		mIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 70000);// 70s
		startActivityForResult(mIntent, 2);// CAMERA_ACTIVITY = 1
	}

	/**
	 * @author lilin
	 * @date 2012-6-20 下午8:39:25
	 * @annotation 调用系统相册
	 */
	private void callSystemCamera() {
		Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
		// intent.putExtra("", value);
		startActivityForResult(intent, 1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		if (requestCode == 1) {// 拍照返回处理
			if (intent != null) {
				getBackPhotoData(requestCode, resultCode, intent);
			}
		}

		if (requestCode == 2) {// 录像返回处理
			if (intent != null) {
				getBackVedioData(requestCode, resultCode, intent);
			}
		}
		super.onActivityResult(requestCode, resultCode, intent);
	}

	/**
	 * @author lilin
	 * @date 2012-12-10 上午10:44:50
	 * @annotation 获取视频数据
	 */
	private void getBackVedioData(int requestCode, int resultCode, Intent intent) {
		if (resultCode != RESULT_OK)
			return;
		final Intent data = intent;
		saveVedio(data);
//		AlertHelp.ShowAlert(this, "是否要添加进附件列表", new OnClickListener() {
//			public void onClick(DialogInterface dialog, int which) {
//				saveVedio(data);
//			}
//		});

	}

	/**
	 * @author lilin
	 * @date 2012-6-20 下午8:57:04
	 * @annotation 获取返回的照片数据
	 */
	private void getBackPhotoData(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		Bundle extras = intent.getExtras();
		Bitmap mBitmap = (Bitmap) extras.get("data");
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
		byte[] mData = byteStream.toByteArray();
		final Bitmap bitmap = BitmapFactory.decodeByteArray(mData, 0,
				mData.length);
		// int tw = Integer.valueOf(getResources().getString(R.string.toW));
		// int ty = Integer.valueOf(getResources().getString(R.string.toH));
		// 现将图片进行缩放处理，再预览，原始是640*480，缩放后是120*90
		// final Bitmap preBitmap = GetBitmap.getZoomRotateBitmap(bitmap, 120,
		// 90,0);
		// newbitmap = GetBitmap.dateBitmap(preBitmap,
		// DateHelp.getFormateDate(2), tw, ty);// 向照片中写入系统日期
		// preView_iv.setImageBitmap(preBitmap);
		savepicture(bitmap, DateHelp.getFormateDate(1));// 保存带日期图片
//		AlertHelp.ShowAlert(this, "是否要添加进附件列表", new OnClickListener() {
//			public void onClick(DialogInterface dialog, int which) {
//				savepicture(bitmap, DateHelp.getFormateDate(1));// 保存带日期图片
//			}
//		});

		// imageView.setImageBitmap(mBitmap);// 把得到的图片绑定在控件上显示、
	}

	// 保存图片
	public void savepicture(Bitmap bitmap, String formateDate) {
		// 格式一：2011-11-1 12-12-66 作为文件名
		String formateDate1 = formateDate;
		try {
			String photoName = formateDate1 + ".jpg";// 照片名称
			String savePath = getString(R.string.photoPath_default);
			if (!SDCard.getFileSavePath(savePath).toString().equals(null)) {
				File file = new File(
						SDCard.getFileSavePath(getString(R.string.photoPath_default)),
						photoName);// 创建图片文件
				FileOutputStream outStream = new FileOutputStream(file);
				bitmap.compress(CompressFormat.JPEG, 100, outStream);// 1-100:图片质量参数
				outStream.close();
				// String text = "成功保存到:" + savePath;
				addTakePicTOAttachList(photoName, savePath + "/" + photoName);// 添加进附件列表
				// ToastHelp.showToast(getApplicationContext(), text);
			} else {
				ToastHelp.showToast(getApplicationContext(), "请插入内存卡！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 保存视频
	public void saveVedio(Intent intent) {
		try {

			String savePath = getString(R.string.vedio_savepath);
			if (!SDCard.getFileSavePath(savePath).toString().equals(null)) {
				AssetFileDescriptor videoAsset = getContentResolver()
						.openAssetFileDescriptor(intent.getData(), "r");
				FileInputStream fis = videoAsset.createInputStream();
				String vediofilename = DateHelp.getFormateDate(3) + ".3gp";
				File tmpFile = new File(SDCard.getFileSavePath(savePath),
						vediofilename);
				FileOutputStream fos = new FileOutputStream(tmpFile);
				byte[] buf = new byte[1024];
				int len;
				while ((len = fis.read(buf)) > 0) {
					fos.write(buf, 0, len);
				}
				fis.close();
				fos.close();

				addTakePicTOAttachList(vediofilename, savePath + "/"
						+ vediofilename);// 添加进附件列表
			} else {
				ToastHelp.showToast(getApplicationContext(), "请插入内存卡！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void refresh(Object... param) {
		if (param[0].toString().equals(MainService.NORMAL)) {

			if (param[1].toString().equals(HttpHelp.RESPONSEINFO_SUCCESS)) {
				epointDialog = EpointDialog.getDialog(this, sendSuccessMsg, 3,
						new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						epointDialog.dismiss();
						finish();
					}
				});
				epointDialog.show();
			} else {
				ToastHelp.showToast(this, sendFailureMsg);
			}

		}
		m_Dialog.dismiss();
	}

	@Override
	public int getLayoutInt() {
		return R.layout.mailsend;
	}

	/* (non-Javadoc)
	 * @see com.epoint.mobileoa.bizlogic.SuperView#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		PersonChooseViewNew.initData();
	}

	@Override
	public void KEYCODE_BACK() {
		back();
	}

	private void back() {
		if (pagetype == 1) {
			Intent it_cgx = new Intent(this, DraftListView.class);
			startActivity(it_cgx);
		}
		finish();
	}

	/* (non-Javadoc)
	 * @see com.epoint.mobileoa.bizlogic.SuperView#onResume()
	 */
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		if (resumeType == 0) {
			String[] info = StringHelp
					.dealChooseListToString(PersonChooseViewNew.clist);
			sjrnames = info[0];
			sjrguids = info[1];
			etSJR.setText(sjrnames);
		} else if (resumeType == 1) {
			String[] info = StringHelp
					.dealChooseListToString(PersonChooseViewNew.clist);
			csrnames = info[0];
			csrguids = info[1];
			etCJR.setText(csrnames);
		} else if (resumeType == 2) {
			// if (FileChooseView.fileMap.size() > 0) {
			// String fileName = FileChooseView.fileMap.get("name");
			// String filePath = FileChooseView.fileMap.get("path");
			// Map<String, Object> map = new HashMap<String, Object>();
			// map.put("name", fileName);
			// map.put("path", filePath);
			// map.put("icon", R.drawable.img_attachlist);
			// if (!attachmData.contains(map)) {
			// attachmData.add(map);
			// } else {
			// ToastHelp.showToast(this, "附件重复添加");
			// }
			// MyAdapter adapter = new MyAdapter(this);
			// lvAttach.setAdapter(adapter);
			// }
			initAttachList();// 初始化附件列表

		}
	
	}

	/**
	 * @author lilin
	 * @date 2012-7-23 上午10:09:05
	 * @annotation
	 */
	private void initAttachList() {

		addSDCardAttach();
		addDownLoadAttach();
		addPicAttach();
		addAudioRecordAttach();

		if (attachmData.size() > 0) {
			System.out.println("附件数量=" + attachmData.size());
			for (int i = 0; i < attachmData.size(); i++) {
				LogHelp.LogI(attachmData.get(i).get("name").toString());
				LogHelp.LogI(attachmData.get(i).get("path").toString());
			}
		}
		MyAdapter adapter = new MyAdapter(this);
		lvAttach.setAdapter(adapter);

	}

	/**
	 * @author lilin
	 * @date 2012-7-23 上午10:54:46
	 * @annotation
	 */
	private void addDownLoadAttach() {

		Map<Integer, Boolean> isSelect = new HashMap<Integer, Boolean>();
		isSelect = AttachManagerListAdp.isSelected;
		if (isSelect != null) {
			if (isSelect.size() > 0) {
				List<Map<String, Object>> mData = AttachListView.mData;
				for (int i = 0; i < isSelect.size(); i++) {
					if (isSelect.get(i) == true) {
						String fileName = mData.get(i).get("title").toString();
						String filePath = mData.get(i).get("path").toString();
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("name", fileName);
						map.put("path", filePath);
						map.put("icon", R.drawable.img_attachlist);
						if (!attachmData.contains(map)) {
							attachmData.add(map);
						} else {
							// ToastHelp.showToast(this, "该附件已存在！");
						}
					}
				}
			}
			AttachListView.mData.clear();
			isSelect = new HashMap<Integer, Boolean>();
			for (int i = 0; i < isSelect.size(); i++) {
				isSelect.put(i, false);
			}
			AttachManagerListAdp.isSelected = isSelect;
		}

	}

	// 添加图片附件
	private void addPicAttach() {

		List<String> chooseFileList = new ArrayList<String>();
		chooseFileList = PhotoListView.chooseFileList;
		if (chooseFileList.size() > 0) {
			for (int i = 0; i < chooseFileList.size(); i++) {
				String fileName = SDCard.getName(chooseFileList.get(i)
						.toString());
				String filePath = chooseFileList.get(i).toString();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("name", fileName);
				map.put("path", filePath);
				map.put("icon", R.drawable.img_attachlist);
				if (!attachmData.contains(map)) {
					attachmData.add(map);
				} else {
					// ToastHelp.showToast(this, "该附件已存在！");
				}
			}
		}
		PhotoListView.chooseFileList.clear();
	}

	// 添加录音附件
	private void addAudioRecordAttach() {

		List<String> chooseFileList = new ArrayList<String>();
		chooseFileList = AudioRecordView.chooseFileList;
		if (chooseFileList.size() > 0) {
			for (int i = 0; i < chooseFileList.size(); i++) {
				String fileName = SDCard.getName(chooseFileList.get(i)
						.toString());
				String filePath = chooseFileList.get(i).toString();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("name", fileName);
				map.put("path", filePath);
				map.put("icon", R.drawable.img_attachlist);
				if (!attachmData.contains(map)) {
					attachmData.add(map);
				} else {
					// ToastHelp.showToast(this, "该附件已存在！");
				}
			}
		}
		AudioRecordView.chooseFileList.clear();
	}

	/**
	 * 
	 * @author lilin
	 * @date 2012-7-23 下午4:01:19
	 * @annotation 添加已拍图片到附件列表
	 */
	public void addTakePicTOAttachList(String filename, String filepath) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", filename);
		map.put("path", filepath);
		map.put("icon", R.drawable.img_attachlist);
		if (!attachmData.contains(map)) {
			attachmData.add(map);
		} else {
			// ToastHelp.showToast(this, "该附件已存在！");
		}
		if (attachmData.size() > 0) {
			LogHelp.LogI("附件数量=" + attachmData.size());
			for (int i = 0; i < attachmData.size(); i++) {
				LogHelp.LogI("name="
						+ attachmData.get(i).get("name").toString());
				LogHelp.LogI("path="
						+ attachmData.get(i).get("path").toString());
			}
		}
		MyAdapter adapter = new MyAdapter(this);
		lvAttach.setAdapter(adapter);
	}

	/**
	 * @author lilin
	 * @date 2012-7-23 上午10:10:22
	 * @annotation
	 */
	private void addSDCardAttach() {

		if (FileChooseView.fileMap.size() > 0) {
			String fileName = FileChooseView.fileMap.get("name");
			String filePath = FileChooseView.fileMap.get("path");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", fileName);
			map.put("path", filePath);
			map.put("icon", R.drawable.img_attachlist);
			if (!attachmData.contains(map)) {
				attachmData.add(map);
			} else {
				// ToastHelp.showToast(this, "该附件已存在！");
			}
		}
		FileChooseView.fileMap.clear();

	}

	private List<Map<String, Object>> attachmData = new ArrayList<Map<String, Object>>();


	public class MyAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return attachmData.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.listviewfile, null);
				holder.fc_img = (ImageView) convertView
						.findViewById(R.id.fc_img);
				holder.fc_title = (TextView) convertView
						.findViewById(R.id.fc_title);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.fc_img.setBackgroundResource((Integer) attachmData.get(
					position).get("icon"));
			holder.fc_title.setText((String) attachmData.get(position).get(
					"name"));
			setTextViewFontColor(holder.fc_title);
			return convertView;
		}
	}

	public final class ViewHolder {
		public ImageView fc_img;
		public TextView fc_title;
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position1, long id) {
		final int fileposition = position1;

		AlertHelp.showAlertMenu(this, "附件",
				new String[] { "打开", "移除", "文件属性" },
				new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (which == 1) {
					attachmData.remove(fileposition);
					MyAdapter adapter = new MyAdapter(MailSendView.this);
					lvAttach.setAdapter(adapter);
				} else if (which == 2) {
					File f = new File(attachmData.get(fileposition)
							.get("path").toString());
					AlertHelp.showFileAttributeAlert(MailSendView.this,
							f);
				} else if (which == 0) {
					String filepath = attachmData.get(fileposition)
							.get("path").toString();
					IOHelp.doOpenFile(MailSendView.this, filepath);
				}
			}

		});
		return super.onItemLongClick(parent, view, position1, id);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		String filepath = attachmData.get(position).get("path").toString();
		IOHelp.doOpenFile(MailSendView.this, filepath);
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}
}